<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Fangattr;
use Illuminate\Http\Request;

class FangAttrController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //实例化
        $model = new Fangattr();
        //取数据
        $data = $model->getList();
        //指定视图并赋值
        return view('admin.fangattr.index',compact('data'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        // 获取顶部属性
        $data = Fangattr::where('pid', 0)->get();
        // 指定视图并赋值
        return view('admin.fangattr.create', compact('data'));
    }

    /**
     * Store a newly created resource in storage.
     *  添加处理
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //表单验证
        $this->validate($request,[
            'name'=>'required',
            //'field_name'=>'ruquired'
        ]);
        //验证通过后，入库并跳转到列表页面
        //获取数据
        $postData = $request->except(['_token','file']);
        //因为字段不能为null，而我们没有传数据，所以一定解决手段
        $postData['field_name'] = !empty($postData['field_name']) ? $postData['field_name']:'';
        //入库
        Fangattr::create($postData);
        //跳转
        return redirect(route('admin.fangattr.index'));

    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Fangattr  $fangattr
     * @return \Illuminate\Http\Response
     */
    public function show(Fangattr $fangattr)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Fangattr  $fangattr
     * @return \Illuminate\Http\Response
     */
    public function edit(Fangattr $fangattr)
    {
        //获取顶部属性
        $data = Fangattr::where('pid',0)->get();
        return view('admin.fangattr.edit',compact('data','fangattr'));
    }

    /**
     * Update the specified resource in storage.
     *  修改处理
     * @param   \Illuminate\Http\Request  $request
     * @param  \App\Models\Fangattr  $fangattr
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Fangattr $fangattr)
    {
        //表单验证
        $this->validate($request,[
            'name'=>'required'
        ]);
        //表单验证后，入库并跳转到列表页
        //获取数据
        $postData = $request->except(['_token','file','_method']);
        //因为字段不能为null，而我们没有传数据，所以一定要解决手段
        $postData['field_name']=!empty($postData['field_name']) ? $postData['field_name'] : '';
        //修改入库
        $fangattr->update($postData);
        //跳转
        return redirect(route('admin.fangattr.index'));



    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Fangattr  $fangattr
     * @return \Illuminate\Http\Response
     */
    public function destroy(Fangattr $fangattr)
    {
        $fangattr->delete();
        return ['status'=>0,'msg'=>'删除成功'];
    }
    //文件上传
    public function upfile(Request $request)
    {
        //默认图标
        $pic = config('up.pic');
        if($request->hasFile('file')){
            //上传
            //参数二 配置节点信息
            $ret = $request->file('file')->store('','fangattr');
            $pic = '/uploads/fangattr/'.$ret;
        }
        return ['status'=>0,'url'=>$pic];
    }
}
